package com.huxin.servelt;

import com.alibaba.fastjson.JSON;
import com.huxin.pojo.UserInfo;
import com.huxin.service.Userservice;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/register")
public class UserRegisterServlet extends HttpServlet {
    private Userservice userservice=new Userservice();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求方法
//        request.setCharacterEncoding("utf-8");
        //处理响应乱码
//        response.setContentType("text/json;charset=utf-8");
        String s = request.getReader().readLine();
        UserInfo userInfo = JSON.parseObject(s, UserInfo.class);
        System.out.println("新增用户："+userInfo);
        if (null==userInfo.getName()||""==userInfo.getName()){
            response.getWriter().write(String.valueOf(0));
        }else {
            int i=userservice.adduser(userInfo);
            response.getWriter().write(String.valueOf(i));
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
